clear
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
sysdir set PLUS /Users/zachbrown/Projects/PriceTransparency/Code/ado


use Raw/ref_tables/provider_detail.dta


mdesc prov_clinic_state
mdesc prov_clinic_zip 
mdesc nh_county_code

gen prov_in_nh = (prov_clinic_state=="NH" | prov_clinic_state=="33" | inrange(nh_county_code,1,19))
gen unsure = inlist(prov_clinic_state,"","..","0","99")


// Fill in by NPI
bys npi: egen prov_in_nh_tmp = max(prov_in_nh)
tab prov_in_nh prov_in_nh_tmp, miss
tab prov_in_nh_tmp unsure, miss
replace prov_in_nh = 1 if prov_in_nh_tmp==1 & unsure==1
drop prov_in_nh_tmp

// Replace when unknown
replace prov_in_nh = . if prov_in_nh==0 & unsure==1
tab prov_clinic_state prov_in_nh, miss


// Fill in by NPI
bys npi: egen prov_notin_nh_tmp = min(prov_in_nh)
tab prov_in_nh prov_notin_nh_tmp, miss
tab prov_notin_nh_tmp unsure, miss
replace prov_in_nh = 0 if prov_notin_nh_tmp==1 & unsure==1
drop prov_notin_nh_tmp

bys npi: egen prov_clinic_zip_tmp = mode(prov_clinic_zip)
replace prov_clinic_zip = prov_clinic_zip_tmp if prov_clinic_zip==. & prov_clinic_zip_tmp~=.
drop prov_clinic_zip_tmp
mdesc prov_clinic_zip

keep prov_key prov_in_nh individual prov_type prov_clinic_zip
save build/provider_build.dta, replace

